<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 搜索板块 -->
        <el-col :span="19">
          <el-form ref="formInline" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="关键字" prop="keyword">
        <el-input v-model="formInline.keyword" placeholder="根据文章标题搜索" size="small"></el-input>
        </el-form-item>
        <el-form-item label="状态" style="margin-left: 30px;"  prop="state">
      <el-select v-model="formInline.state" placeholder="请选择" size="small">
        <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
      </el-form-item>
    <el-form-item>
      <el-button size="small" @click="formInlines">清除</el-button>
      <el-button type="primary" size="small" @click="search">搜索</el-button>
    </el-form-item>
  </el-form>
</el-col>
      <el-col :span="5">
          <el-button
            size="small"
            type="success"
            style="float: right"
            icon="el-icon-edit"
            @click="addDialog"
          >
            新增技巧
        </el-button>
      </el-col>
        <!-- 消息alert提示 -->
         <div style="margin-bottom: 15px;">
           <el-alert
             :title="`数据一共 ${counts} 条`"
             type="info"
             show-icon
             :closable="false">
           </el-alert>
         </div>
      <!-- 表格内容 -->
      <el-table :data="tablelist" style="width: 100%">
        <el-table-column label="序号" :index="indexMethod" type="index" width="80px"></el-table-column>
        <el-table-column label="文章标题" prop="title" width="380px">
          <template slot-scope="scope" >
              {{ scope.row.title }}
              <i
                @click="video(scope.row.videoURL)"
                v-if="scope.row.videoURL"
                class="el-icon-film"
                style="color: #0000ff; cursor:pointer; font-size:18px;"
                aria-hidden="true"
              ></i>
            </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"></el-table-column>
        <el-table-column label="录入人" prop="username"></el-table-column>
        <el-table-column label="录入时间" prop="createTime">
          <template #default="{ row }">
             {{formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100px" prop="state">
          <template slot-scope="{ row }">
            {{ row.state === 0 ? "已禁用" : "已启用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
           <template #default="{row}">
            <el-button type="text" @click="preveiew(row)">预览</el-button>
            <el-button type="text" @click="handleClick(row)">
              {{row.state === 0 ? "启用" : '禁用'}}
            </el-button>
            <el-button type="text"  @click=" modifyClick(row)" :disabled="row.state === 1 ">修改</el-button>
            <el-button type="text" @click="removeApi(row)"  :disabled="row.state === 1 ">删除</el-button>
           </template>
        </el-table-column>
    </el-table>
    <!-- 分页内容区 -->
     <div style="padding: 10px; display: flex; justify-content: flex-end">
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 50]"
        layout="prev, pager, next,sizes,jumper"
        :total="counts">
      </el-pagination>
     </div>
    </el-card>
    <!-- 弹窗显示区域 -->
    <el-dialog :title="showTitle" @close="closeDialog" :close-on-click-modal="false" :visible="showDialog">
    <!-- 表单 -->
    <el-form label-width="120px" ref="ruleForm" :model="formData" :rules="rules">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formData.title" width="50%" placeholder="请输入文章标题" />
      </el-form-item>
      <el-form-item label="文章内容"  prop="articleBody">
      <quill-editor v-model="formData.articleBody" :options="editorOption" @blur="$refs.ruleForm.validateField('articleBody')" ></quill-editor>
      </el-form-item>
      <el-form-item  label="视频地址" style="margin-top: 10px;">
        <el-input width="50%" v-model="formData.videoURL" placeholder="请输入视频地址">
        </el-input>
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
     <div style="text-align: right;">
       <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="addModifications">确定</el-button>
     </div>
    </template>
  </el-dialog>
  <!-- 预览弹窗 -->
  <el-dialog title="预览文章" @close="previewArticles" :close-on-click-modal="false" :visible="previewArticle">
       <el-form label-width="120px" :model="formData">
         <!-- 标题 -->
        <h2>{{formData.title}}</h2>
      <!-- 时间，浏览量，录入人 -->
      <div class="sjianllllrr">
        <i>{{formatDate(formData.createTime) }}</i>
        <i>超级管理员</i>
        <i class="el-icon-view">{{formData.visits}}</i>
      </div>
       <div class="neirquyu">
        <i v-html="formData.articleBody"></i>
       </div>
     </el-form>
  </el-dialog>
    <!-- 视频弹窗 -->
       <div class="video" v-if="videoUrl">
        <div class="video-ico" @click="closeVideo()"><span class="el-icon-close" /></div>
        <div class="video-box">
          <video autoplay :src="videoUrl"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { list, remove, add, detail, update } from '@/api/hmmm/articles.js'
import { status } from '@/api/hmmm/constants'
import dayjs from 'dayjs'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  data () {
    return {
      number: {
        page: 1,
        pagesize: 10
      },
      status,
      input: '',
      select: '',
      counts: null,
      formInline: {
        keyword: null,
        state: ''
      },
      tablelist: [],
      showDialog: false,
      previewArticle: false,
      videoUrl: null,
      formData: {
        title: '',
        articleBody: '',
        id: '',
        createTime: '',
        username: '',
        videoURL: '',
        visits: '',
        state: ''
      },
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote', 'code-block'], // 引用，代码块
            [{ align: [] }], // 对齐方式
            ['image', 'video'] // 上传图片、上传视频
          ]
        },
        placeholder: ''
      }, // 编辑器配置项
      // 校验
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: ['blur', 'change'] }
        ],
        articleBody: [
          { required: true, message: '文章内容不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  components: {
    quillEditor
  },
  computed: {
    // 修改标题
    showTitle () {
      return this.formData.id ? '修改文章' : '新增文章'
    }
  },
  created () {
    this.listApi()
  },
  methods: {
    // 关闭视频展示
    closeVideo () {
      this.videoUrl = null
    },
    // 开启视频弹窗
    video (row) {
      this.videoUrl = row
    },
    // 禁用和启用
    handleClick (row) {
      if (row.state === 1) {
        row.state = 0
      } else {
        row.state = 1
      }
      // 发起请求，更新后台数据
      // await changeState(row)
      this.$message.success('操作成功')
    },
    // 关闭预览弹窗
    previewArticles () {
      this.previewArticle = false
    },
    // 预览弹窗
    async preveiew (row) {
      this.previewArticle = true
      const { data } = await detail(row)
      this.formData = data
    },
    // 关闭弹窗
    closeDialog () {
      this.showDialog = false
      this.$refs.ruleForm.resetFields()
      this.formData = {
        title: '',
        articleBody: ''
      }
    },
    // 添加成功与修改成功
    addModifications () {
      this.$refs.ruleForm.validate(async flag => {
        if (!flag) return
        if (this.formData.id) {
          await update(this.formData)
          // 添加完成之后 成功提示+关闭弹框（重置数据）+重新渲染列表
          this.$message.success('恭喜！修改成功')
        } else {
          await add(this.formData)
          // 添加完成之后 成功提示+关闭弹框（重置数据）+重新渲染列表
          this.$message.success('恭喜！添加成功')
        }
        this.listApi()
        this.closeDialog()
      })
    },
    // 文章列表
    async listApi () {
      const { data } = await list(this.number)
      this.tablelist = data.items
      this.counts = data.counts
    },
    // 页的尺寸
    handleSizeChange (val) {
      // console.log(val)
      this.number.pagesize = val
      this.listApi()
    },
    // 页数
    handleCurrentChange (val) {
      this.number.page = val
      this.listApi()
    },
    // 序号
    indexMethod (index) {
      return (this.number.page - 1) * this.number.pagesize + index + 1
    },
    // 处理时间格式
    formatDate (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 删除功能
    removeApi (row) {
      // console.log(row)
      this.$confirm('此操作将永久删除该文章, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.$message.success('删除成功')
        this.listApi()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 搜索功能
    async search () {
      this.number.page = 1
      const param = { ...this.number, ...this.formInline }
      if (!param.keyword) {
        param.keyword = null
      }
      if (!param.state) {
        param.state = null
      }
      const { data } = await list(param)
      this.tablelist = data.items
      this.counts = data.counts
    },
    // 清除功能
    formInlines () {
      this.formInline = {
        keyword: '',
        state: ''
      }
      this.listApi()
    },
    // 数据回填
    async modifyClick (row) {
      this.showDialog = true
      const { data } = await detail(row)
      this.formData = data
    },
    addDialog () {
      this.showDialog = true
    }
  }
}

</script>

<style lang="scss" scoped>
.ivu-upload {
    display: none;
}
.quill-editor{
   ::v-deep .ql-editor{
    height: 250px;
  }
}
.sjianllllrr{
  i {
    margin: 5px;
    font-style: normal;
  }
}
.neirquyu{
  border-top: 1px dashed #cccccc;
  background-color: #f5f5f5;
  // height: 100px;
  width: 100%;
  display: inline-block;
  font-size: 16px;
  margin-top: 10px;

  padding: 5px;
  i{
    font-style: normal;

  }
}
.video {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .video-ico {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .video-box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
